//
//  XGResetPasswordViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/5/11.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import LeanCloud
import MBProgressHUD

class XGResetPasswordViewController: UIViewController
{
    override func loadView()
    {
        super.loadView()
        setUpUI()
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        navigationItem.title = "重置密码"
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChangeNotification), name: UITextField.textDidChangeNotification, object: nil)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        view.endEditing(true)
    }
    
    /// 邮箱
    private lazy var emailTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "请输入账号绑定的邮箱"
        textField.clearButtonMode = .whileEditing
        textField.borderStyle = .roundedRect
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.keyboardType = .emailAddress
        return textField
    }()
    /// 重置按钮
    private lazy var resetButton: UIButton = { [weak self] in
        let button = UIButton(title: "重置", fontSize: 20, normalColor: UIColor.white, highlightedColor: UIColor.white, target: self, action: #selector(resetPasswordAction))
        button.setBackgroundImage(UIColor.colorWithHexString(hexadecimal: "#FF6A6A").toImage(), for: .normal)
        button.setBackgroundImage(UIColor.colorWithHexString(hexadecimal: "#FFAEB9").toImage(), for: .disabled)
        button.isEnabled = false
        button.layer.cornerRadius = 20
        button.layer.masksToBounds = true
        return button
    }()

}

extension XGResetPasswordViewController: XGUserAccountValidateProtocol
{
    @objc func resetPasswordAction() -> Void
    {
        view.endEditing(true)
        if !validateEmail(email: emailTextField.text!) {
            MBProgressHUD.showErrorTips(text: "邮箱格式不正确!")
            return
        }
        
        LCUser.requestPasswordReset(email: emailTextField.text!) { (result) in
            switch result {
            case .success:
                MBProgressHUD.showSuccessTips(text: "已将重置密码邮件发送至您的邮箱！")
                DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                    self.navigationController?.popViewController(animated: true)
                }
            case .failure(error: _):
                MBProgressHUD.showErrorTips(text: "发送重置邮件失败!")
            }
        }
    }
    
    /// textField通知监听方法
    @objc func textDidChangeNotification() -> Void
    {
        resetButton.isEnabled = emailTextField.hasText 
    }
    
    func setUpUI() -> Void
    {
        view.backgroundColor = UIColor.white
        view.addSubview(emailTextField);
        view.addSubview(resetButton)
        
        emailTextField.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.topMargin).offset(30)
            make.left.equalToSuperview().offset(30)
            make.right.equalToSuperview().offset(-30)
            make.height.equalTo(44)
        }
        resetButton.snp.makeConstraints { (make) in
            make.top.equalTo(emailTextField.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: ceil(TodayNews_Screen_Width * 0.6), height: 44))
        }
    }
}
